package com.yummy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yummy.dto.QueryOrderVo;

import com.yummy.entity.OrderDetail;
import com.yummy.entity.Orders;
import com.yummy.mapper.OrderDetailMapper;
import com.yummy.mapper.OrdersMapper;
import com.yummy.service.OrdersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {


    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;


    Logger logger = LoggerFactory.getLogger(OrdersServiceImpl.class);

    /*单个订单id查询*/
    @Override
    public Orders selectOrderByOrderId(long orderId) {
        Orders orders = new Orders();
        orders.setId(orderId);
         List<Orders> order= ordersMapper.select(orders);
        /*查询结果应该只有一个*/
        /*System.out.println(order+"打印一下order");*/
        if(order.size()!=0){

            return order.get(0);
        }
        return null;
    }

    @Override
    public List<OrderDetail> selectOrderDetailByOrderId(long orderId) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(orderId);
        return orderDetailMapper.select(orderDetail);
    }

    /*指定订单改变订单状态*/
    @Override
    public int modifyOrderById(Orders orders) {
        return ordersMapper.updateByPrimaryKeySelective(orders);

    }

    /*一个订单 或多个订单 修改至取消订单*/
    @Override
    public int modifyOrderForDeleteByPrimaryKey(String... ids) {


        return ordersMapper.updateForDeleteByPrimaryKey(ids);
    }

    @Override
    public int addOrder(Orders orders) {
        return ordersMapper.insertOrder(orders);
    }

    @Override
    public boolean updateState(String tradeNo, int status) {

        return ordersMapper.updateStateByNumber(tradeNo,status)>0;
    }
    /*根据页面表单信息修改order数据*/

    @Override
    public int modifyOrder(Orders orders) {
        return ordersMapper.updateByPrimaryKeySelective(orders);
    }

    /*查询顾客用户的订单信息*/
    @Override
    public PageInfo<Orders> selectOrderByUserId(long userId) {
        Page<Orders> page =PageHelper.startPage(1,6);
        Orders orders = new Orders();
        orders.setUserId(userId);
        ordersMapper.select(orders);
        PageInfo pageInfo = page.toPageInfo();
        return pageInfo;
    }


    /*查询店铺订单信息*/
    @Override
    public PageInfo<Orders> selectAllOrder(int accountId, QueryOrderVo vo) {
        Page<Orders> page = PageHelper.startPage(vo.getCurrentPage(), vo.getPageSize());
        logger.info(vo.toString());

        Orders orders = new Orders();
        orders.setAccountId(accountId);

        ordersMapper.select(orders);
        PageInfo pageInfo = page.toPageInfo();
        return pageInfo;
    }

    @Override
    public List<Orders> selectAllOrderOfUser(int userId) {

        List<Orders> pageInfo = ordersMapper.selectAllOrderOfUser(userId);

        return pageInfo;
    }
    @Override
    public List<Orders> selectAllOrderOfUser2(int userId) {

        List<Orders> pageInfo = ordersMapper.selectAllOrderOfUser2(userId);

        return pageInfo;
    }

    @Override
    public List<Orders> selectAllOrderOfUser3(int userId) {

        List<Orders> pageInfo = ordersMapper.selectAllOrderOfUser3(userId);

        return pageInfo;
    }

    @Override
    public List<Orders> selectAllOrderOfUser4(int userId) {

        List<Orders> pageInfo = ordersMapper.selectAllOrderOfUser4(userId);

        return pageInfo;
    }



    @Override
    public int selectOrderAccountOfUser(int userId) {
        return ordersMapper.selectCountOfUnpay(userId);
    }

    @Override
    public int selectOrderAccountOfUser2(int userId) {
        return ordersMapper.selectCountOfUnpay2(userId);
    }

    @Override
    public int selectOrderAccountOfUser3(int userId) {
        return ordersMapper.selectCountOfUnpay3(userId);
    }

    @Override
    public PageInfo<Orders> selectAllOrderByCondition(int accountId, QueryOrderVo vo) {
        Page<Orders> page = PageHelper.startPage(vo.getCurrentPage(), vo.getPageSize());
        logger.info(vo.toString());
        vo.setStoreId(accountId);
        ordersMapper.selectOrderByCondition(vo);
        PageInfo pageInfo = page.toPageInfo();
        return pageInfo;
    }





}
